<template>
  <base-breadcrumb>
    <searchData :search-data="searchArr" @search="search" />

    <a-card :bordered="false" style="over-flow:scroll">
      <list-table ref="table" size="default" row-key="row.id" :columns="columns" :request="loadData" :alert="true" show-size-changer show-quick-jumper show-pagination="auto">
        <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
        <span slot="action" slot-scope="text, record">
          <MoreButton>
            <a v-btnPermission="'pm_063_edi_02'" v-if="hideBuquBtn()" @click="handleEdit(record)">配置考核时间</a>
          </MoreButton>
        </span>
      </list-table>
    </a-card>

    <a-modal v-if="visible" destroy-on-close title="分包商考核时间配置" :visible="true" width="1000px" @cancel="visible = false">
      <template slot="footer">
        <a-button key="back" @click="handleCancel">
          取消
        </a-button>
        <a-button key="submit" :loading="loading" type="primary" @click="handleOk">
          确认
        </a-button>
      </template>
      <a-form-model ref="ruleForm" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }" :rules="rules" :model="form">
        <a-row>
          <a-col :md="12" :sm="12">
            <a-form-model-item label="月份" prop="month">
              <a-input v-model="form.month" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-model-item label="本月是否参加考核" prop="isAssess">
              <a-select v-model="form.isAssess" placeholder="请选择">
                <a-select-option :value="1">是</a-select-option>
                <a-select-option :value="0">否</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <span style="color: red">注意：若某月份分包商在项目已出场，则无论设置是否参与考核，该分包商都不参与考核</span>
      </a-form-model>
    </a-modal>
  </base-breadcrumb>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getassessmentList, addassessmentList, editassessmentList, statistics, getDetails } from '@/api/subcontractors/managerAssessment'
import { getSubcontractorsList, entryExitLedgerAssessmentTime } from '@/api/subcontractors/standing'
import { themeChoose } from '@/mixin/mixin'
import UploadList from '@/components/UploadList'
import ApprovalDetails from '@/components/approvalDetails'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import moment from 'moment'
export default {
  name: 'a' + Date.now(),
  mixins: [themeChoose],
  components: {
    SelectSubCompany,
    SelectProject,
    STable,
    UploadList,
    ApprovalDetails
  },
  data() {
    return {
      columns: [
        {
          title: '序号',
          width: 65,
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '分包商名称',
          width: 120,
          dataIndex: 'subcontractors'
        },
        {
          title: '项目名称',
          width: 100,
          dataIndex: 'projectName'
        },
        {
          title: '分公司',
          width: 100,
          dataIndex: 'branch'
        },
        {
          title: '合作事项',
          dataIndex: 'matters',
          width: 100,
          customRender: (text, record, index) => {
            return this.processTreeSelect(text)
          }
        },
        {
          title: '进场时间',
          width: 100,
          dataIndex: 'entryTime'
        },
        {
          title: '定标时间',
          width: 100,
          dataIndex: 'calibrationTime'
        },
        {
          title: '出场时间',
          width: 100,
          dataIndex: 'outTime'
        },
        {
          title: '创建时间',
          width: 100,
          dataIndex: 'createTime'
        },
        {
          title: '状态',
          width: 100,
          dataIndex: 'statusName'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 150,
          scopedSlots: { customRender: 'action' }
        }
      ],
      disabled: false,
      type: '',
      title: '',
      loading: false,
      form: {
        id: null,
        month: null,
        isAssess: null
      },
      visible: false, // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({ statusCodeList: [2, 3, 4] }, parameter, this.queryParam)
        return getSubcontractorsList(requestParameters).then(res => {
          return res
        })
      },
      // 校验规则
      rules: {
        isAssess: [{ required: true, message: '请选择本月是否参加考核', trigger: 'change' }]
      },
      searchArr: [
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect
        },
        {
          name: '分包商名称',
          value: 'subcontractorName',
          type: 'input'
        },
        {
          name: '状态',
          type: 'select',
          value: 'statusCode',
          data: [
            {
              value: '2',
              label: '进场'
            },
            {
              value: '5',
              label: '出场'
            },
            {
              value: '3',
              label: '出场-审批中'
            },
            {
              value: '4',
              label: '出场-审批不通过'
            }
            // {
            //   value: '0',
            //   label: '进场-审批中'
            // },
            // {
            //   value: '1',
            //   label: '进场-审批不通过'
            // },
          ]
        }
      ]
    }
  },
  async created() {
    let year = moment().get('year')
    let date = moment().get('date')
    let month = moment().get('month') + 1
    if (date >= 25) {
      if (month === 12) {
        year = year + 1
        month = 1
      } else {
        month++
      }
    }
    this.form.month = year + '-' + (month > 9 ? month : '0' + month)
  },
  methods: {
    search(values) {
      this.queryParam = values
      this.$refs.table.refresh(true)
    },
    // 表格行点击【考核时间配置】
    handleEdit(record) {
      this.visible = true
      this.form.id = record.id

      // 如果行数据有值，直接等于行数据
      if (record.isAssess !== undefined && record.isAssess !== null && record.month !== undefined && record.month !== null) {
        this.form.isAssess = record.isAssess
        return
      }

      if (record.statusCode == 2 || record.statusCode == 3 || record.statusCode == 4 || (record.statusCode == 5 && record.outTime.includes(this.form.month))) {
        this.form.isAssess = 1
      } else {
        this.form.isAssess = 0
      }
    },
    // 【考核时间配置】弹窗确认按钮
    handleOk() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.loading = true
          entryExitLedgerAssessmentTime(this.form)
            .then(res => {
              this.loading = false
              this.visible = false
              if (res.code == 200) {
                this.$message.success('配置成功')
              }
              this.$refs.table.refresh(true)
            })
            .finally(() => {
              this.loading = false
            })
        } else {
          this.$message.warn('请填写完所有必填项信息')
        }
      })
    },
    handleCancel() {
      this.visible = false
    },
    processTreeSelect(val) {
      if (val) {
        try {
          let r = JSON.parse(val)
          return r.map(v => v.label).join(',')
        } catch (e) {
          return val
        }
      }
      return ''
    }
  }
}
</script>

<style scoped>
.input {
  width: 100%;
}
/*/deep/ .ant-table-pagination.ant-pagination{*/
/*  display:none*/
/*}*/
</style>
